package com.lzq.course.server.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lzq.course.server.domain.Course;
import com.lzq.course.server.domain.CourseContent;
import com.lzq.course.server.domain.CourseExample;
import com.lzq.course.server.dto.*;
import com.lzq.course.server.enums.CourseStatusEnum;
import com.lzq.course.server.mapper.CourseContentMapper;
import com.lzq.course.server.mapper.CourseMapper;
import com.lzq.course.server.mapper.my.MyCourseMapper;
import com.lzq.course.server.util.CopyUtil;
import com.lzq.course.server.util.UuidUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.Date;

@Service
public class CourseService {

    @Resource
    private CourseMapper courseMapper;
    @Resource
    private MyCourseMapper myCourseMapper;
    @Resource
    private CourseCategoryService courseCategoryService;
    @Resource
    private CourseContentMapper courseContentMapper;
    @Resource
    private TeacherService teacherService;

    @Resource
    private ChapterService chapterService;

    @Resource
    private SectionService sectionService;

    /**
     * 有分类ID 根据一级分类查 或者二级分类查 前端传参
     * 无分类ID 查所有课程
     */
    public void list(CoursePageDto pageDto) {
        // 分页查询 页码不是从0开始
        PageHelper.startPage(pageDto.getPage(), pageDto.getSize());

        // 自定义sql查询
        List<CourseDto> courseList = myCourseMapper.list(pageDto);

        PageInfo<CourseDto> pageInfo = new PageInfo<>(courseList);
        pageDto.setTotal(pageInfo.getTotal());

        List<CourseDto> courseDtoList = CopyUtil.copyList(courseList, CourseDto.class);

        pageDto.setList(courseDtoList);
    }

    /**
     * 保存 修改
     * @param courseDto
     */
    @Transactional
    public void save(CourseDto courseDto) {
        //BeanUtils.copyProperties(courseDto, course);
        Course copyCourse = CopyUtil.copy(courseDto, Course.class);

        if (StringUtils.isEmpty(courseDto.getId())) {
            // 新增
            // 八位的 id
            copyCourse.setId(UuidUtil.getShortUuid());

            Date now = new Date();

            courseMapper.insert(copyCourse);
        } else {
            // 编辑
            courseMapper.updateByPrimaryKey(copyCourse);
        }

        // 保存分类
        courseCategoryService.saveBach(copyCourse.getId(), courseDto.getCategorys());
    }

    /**
     * 删除
     * @param id
     */
    public void delete(String id) {
        courseMapper.deleteByPrimaryKey(id);
    }

    /**
     * 更新课程时间
     * @param courseId
     */
    public void updateTime(String courseId) {
        myCourseMapper.updateTime(courseId);
    }

    /**
     * 查询课程内容
     * @param id
     */
    public CourseContentDto findContent(String id) {
        CourseContent content = courseContentMapper.selectByPrimaryKey(id);
        if (StringUtils.isEmpty(content)) {
            return null;
        }
        return CopyUtil.copy(content, CourseContentDto.class);
    }

    /**
     * 保存 课程内容 包含新增和修改
     * @param contentDto
     * @return
     */
    public int saveContent(CourseContentDto contentDto) {
        CourseContent content = CopyUtil.copy(contentDto, CourseContent.class);
        // 修改
        int i = courseContentMapper.updateByPrimaryKeyWithBLOBs(content);
        if (i == 0) {
            // 新增
            i = courseContentMapper.insert(content);
        }
        return i;
    }

    /**
     * 课程排序
     * @param sortDto
     */
    public void sort(SortDto sortDto) {
        // 先更新此课程的顺序
        myCourseMapper.updateSort(sortDto);
        // 在排其他课程的顺序
        // 排序变大
        if (sortDto.getOldValue() < sortDto.getNewValue()) {
            myCourseMapper.moveSortForward(sortDto);
        }

        // 排序变小
        if (sortDto.getOldValue() > sortDto.getNewValue()) {
            myCourseMapper.moveSortBackward(sortDto);
        }
    }

    /**
     * 查询最新发布的三门课程
     * @param pageDto
     * @return
     */
    public void listNew(PageDto pageDto) {
        PageHelper.startPage(pageDto.getPage(), pageDto.getSize());
        // 排序
        CourseExample courseExample = new CourseExample();
        courseExample.createCriteria().andStatusEqualTo(CourseStatusEnum.PUBLISH.getCode());
        courseExample.setOrderByClause("created_at desc");

        // 查询
        List<Course> courseList = courseMapper.selectByExample(courseExample);

        PageInfo<Course> pageInfo = new PageInfo<>(courseList);
        pageDto.setTotal(pageInfo.getTotal());

        List<CourseDto> courseDtoList = CopyUtil.copyList(courseList, CourseDto.class);

        pageDto.setList(courseDtoList);
    }

    /**
     * 查找某一课程，供web模块用，只能查已发布的
     * @param id
     * @return
     */
    public CourseDto findCourse(String id) {
        Course course = courseMapper.selectByPrimaryKey(id);
        if (course == null || !CourseStatusEnum.PUBLISH.getCode().equals(course.getStatus())) {
            return null;
        }
        CourseDto courseDto = CopyUtil.copy(course, CourseDto.class);

        // 查询内容
        CourseContent content = courseContentMapper.selectByPrimaryKey(id);
        if (content != null) {
            courseDto.setContent(content.getContent());
        }

        // 查找讲师信息
        TeacherDto teacherDto = teacherService.findById(courseDto.getTeacherId());
        courseDto.setTeacher(teacherDto);

        // 查找章信息
        List<ChapterDto> chapterDtoList = chapterService.listByCourse(id);
        courseDto.setChapters(chapterDtoList);

        // 查找节信息
        List<SectionDto> sectionDtoList = sectionService.listByCourse(id);
        courseDto.setSections(sectionDtoList);

        return courseDto;
    }
}
